import { ys } from "ys-ts";
import { cfgApp, initApp, selectOrAddTab } from "./app";
import { useRoute, useRouter as baseRouter } from "vue-router";

export interface IToNameConfig {
  name: string;
  params?: any;
}

export function useRouter() {
  const router = baseRouter();
  const route = useRoute();

  async function toName(name: string, params?: any) {
    if (router.hasRoute(name)) {
      await router.push({
        name: name,
        params: params,
      });
    } else {
      console.error(`[router] ${name} 不存在`);
    }
  }

  return {
    router,
    route,
    toName,
  };
}

export function initRouter() {
  console.log(`[router] init`);

  const routerUtil = ys.getRouterUtil();

  initApp();
  routerUtil.addConfig(cfgApp.build());
  routerUtil.addBeforeEach(selectOrAddTab);
}

export function createRouter() {
  initRouter();
  return ys.getRouterUtil().build();
}
